using System;
using System.Collections.Generic;
using System.Text;
using BTV.TVAnyTime;
using System.Collections;

namespace BTV.Base
{
    public class RelatedProgsCollection : List<TVAnyProgram>
    {
        private TVAnyProgram _baseProgram;

        public TVAnyProgram BaseProgram
        {
            get { return _baseProgram; }
            set { _baseProgram = value; }
        }
        private int _maxSize;

        public int MaxSize
        {
            get { return _maxSize; }
            set { _maxSize = value; }
        }

        private int _lowestTotal;

        public int LowestTotal
        {
            get { return _lowestTotal; }
            set { _lowestTotal = value; }
        }

        private int _lowestIndex;

        public int LowestIndex
        {
            get { return _lowestIndex; }
            set { _lowestIndex = value; }
        }

        public RelatedProgsCollection()
            : base()
        {
            MaxSize = 10;
            LowestIndex = -1;
            LowestTotal = -1;
        }

        public RelatedProgsCollection(int size)
            : base(size)
        {
            MaxSize = size;
            LowestIndex = -1;
            LowestTotal = -1;
        }

        public RelatedProgsCollection(TVAnyProgram program)
            : base()
        {
            BaseProgram = program;
            MaxSize = 10;
            LowestIndex = -1;
            LowestTotal = -1;
        }

        public RelatedProgsCollection(TVAnyProgram program, int size)
            : base()
        {
            BaseProgram = program;
            MaxSize = size;
            LowestIndex = -1;
            LowestTotal = -1;
        }

        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1062:ValidateArgumentsOfPublicMethods")]
        public void AddProgram(TVAnyProgram program)
        {
            if (PassValidation(program))
            {
                int score = program.RelatedGenreScore(BaseProgram);
                if (Count == MaxSize)
                {/*check remove and add*/
                    if (score > LowestTotal)
                    {
                        RemoveAt(LowestIndex);
                        Insert(LowestIndex, program);
                        LowestIndex = GetLowestScoreIndex();
                        LowestTotal = this[LowestIndex].RelatedScore;
                    }
                }
                else
                {/*add*/
                    if (LowestTotal == -1)
                    {
                        /*add first*/
                        Add(program);
                        LowestTotal = score;
                        LowestIndex = 0;
                    }
                    else
                    {/*add other*/
                        if (score <= LowestTotal)
                        {
                            LowestIndex = Count;
                        }
                        Add(program);
                    }
                }
            }
        }

        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1011:ConsiderPassingBaseTypesAsParameters")]
        public bool PassValidation(TVAnyProgram p)
        {
            if (p.Equals(BaseProgram))
            {
                return false;
            }
            if (p.Name.Equals(BaseProgram.Name))
            {
                return false;
            }
            foreach (TVAnyProgram prog in this)
            {
                if (p.Name.Equals(prog.Name))
                {
                    return false;
                }
            }
            return true;
        }

        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1024:UsePropertiesWhereAppropriate")]
        public int GetLowestScoreIndex()
        {
            int score = this[0].RelatedScore;
            int index = 0;
            for (int i = 1; i < Count; i++)
            {
                if (this[i].RelatedScore < score)
                    index = i;
            }
            return index;
        }
    }
}
